import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { FormatResponseInterceptor } from './format-response.interceptor';
import { InvokeRecordInterceptor } from './invoke-record.interceptor';
import { CustomExceptionFilter } from './custom-exception.filter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 全局管道 验证参数 验证参数是否符合要求
  app.useGlobalPipes(new ValidationPipe());
  // 全局拦截 格式化响应
  app.useGlobalInterceptors(new FormatResponseInterceptor());
  // 全局拦截 日志记录
  app.useGlobalInterceptors(new InvokeRecordInterceptor());
  // 全局异常捕获
  app.useGlobalFilters(new CustomExceptionFilter());

  // 引入cors
  app.enableCors();

  // 引入swagger
  const config = new DocumentBuilder()
    .setTitle('会议室预订系统')
    .setDescription('api 接口文档')
    .setVersion('1.0')
    .addOAuth2({
      type: 'oauth2',
    })
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api-doc', app, document);
  // 获取配置文件 端口号
  const configService = app.get(ConfigService);
  await app.listen(configService.get('nest_server_port'));
}
bootstrap();
